<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/5/6  15:34
// +----------------------------------------------------------------------
namespace Admin\Controller;
class BrokenController extends CashierBaseController {

    function index() {
        $this->display();
    }
    //破损书处理
    function doBroken(){
        $data=I("post.");
        $listarr=explode("," ,$data['liststr']);
//        var_dump($data);
//        var_dump($listarr);
//        exit;

//        array(9) {
//            ["card_id"]=>"16000067"
//            ["tm"]=>""
//            ["broken_disc"]=>"0.8"
//            ["money"]=>"33.60"
//            ["money_real"]=>"40"
//            ["money_zero"]=>"6.40"
//            ["pay_way"]=>"cash"
//            ["user_id"]=>"34325"
//            ["liststr"]=> "14844712;392;10.00;8.00,14844712;1611;32.00;25.60"
//        }
//        array(2) {
//            [0]=> "
//                14844712;
//                392;
//                10.00;
//                8.00
//            "
//            [1]=> "14844712;1611;32.00;25.60"
//        }

        $list=[];
        foreach($listarr as $k=>$v){
            $tmp=explode(";" ,$v);
            $t['order_id']=$tmp[0];
            $t['book_id']=$tmp[1];
            $t['money_original']=$tmp[2];
            $t['money']=$tmp[3];
            array_push($list,$t);
        }
        $data['liststr']=$list;
        $Orderhb=M("Order_has_book");
        $Orderhb->startTrans();
        $map=[
            "type"=>1,
            "is_backed"=>0
        ];
        $summary="线下破损书回收，回收图书列表:";
        //更新借阅订单破损回收数量
        foreach($data['liststr'] as $k=>$v){
            $map['order_id']=$v['order_id'];
            $map['book_id']=$v['book_id'];
            $d=$Orderhb->where($map)->find();
            $Orderhb->where($map)->setInc("num_broken",1);
            $ud=$Orderhb->where($map)->find();
            if($ud['num_broken'] != $d['num_broken']+1){
                $this->error($d['num_broken']."系统错误,回收失败".$ud['num_broken']);
            }
            if($ud['quantity'] < $ud['num_broken']+$ud['num_backed']){
                $this->error("回收失败,已有图书归还或回收");
            }
            if($ud['quantity'] == $ud['num_broken']+$ud['num_backed']){
                $r=$Orderhb->where($map)->data(["is_backed"=>1])->save();
                if(!$r){
                    $this->error("系统错误,回收失败,无法更新状态");
                }
            }
            $summary.="订单号:".$v['order_id'].",id:".$v['book_id'].",原价:".$v['money_original'].",回收价:".$v['money'].";";
        }
        //创建破损订单
        $map_pay=[
            "user_id"=>$data['user_id'],
            "status"=>1,
            "type"=>5,
            "way"=>$data['pay_way'],
            "summary"=>$summary,
            "money"=>$data['money'],
            "money_real"=>$data['money_real'],
            "money_zero"=>$data['money_zero']
        ];
        $pay_id=M("Pay")->data($map_pay)->add();
        if(!$pay_id){
            $this->error("系统错误");
        }
        foreach($data['liststr'] as $k=>$v){
            $dataList[] = [
                'order_id'=>$v['order_id'],
                'book_id'=>$v['book_id'],
                "user_id"=>$data['user_id'],
                "money"=>$v['money'],
                "money_original"=>$v['money_original'],
                "state_order"=>1,
                "pay_id"=>$pay_id
            ];
        }
        $r=M("Order_broken")->addAll($dataList);
        if($r){
            $Orderhb->commit();
            $this->success("回收成功",U("orderShow","pay_id=".$pay_id));
        }else{
            $Orderhb->rollback();
            $this->error("系统错误");
        }
    }

    //订单打印
    function orderShow(){
        $pay_id=I("get.pay_id");
        $do=M("Order_broken")->alias("a")->where(["a.pay_id"=>$pay_id])->join("book b on a.book_id=b.book_id","left")->field("a.*,b.sm")->order("order_broken_id desc")->select();
        $dp=M("Pay")->where(["pay_id"=>$pay_id])->find();
        $d=[
            "order_id"=>$do[0]['order_broken_id'],
            "pay_id"=>$do[0]['pay_id'],
            "create_time"=>$dp['create_time'],
            "money_b"=>$dp['money'],  //总计
            "discount_buy"=>1, //折扣
            "money"=>$dp['money'],
            'money_real' => $dp['money_real'], //实收
            'money_zero' => $dp['money_zero'], //找零
            "list"=>$do
        ];

        $user_id=$dp['user_id']==''?0:$dp['user_id'];
        $User= new \Common\Model\UserModel();
        $d['card']=$User->getCard($user_id);
        $d['score']=$User->getScore($user_id);

        $this->assign("d",$d);
        $this->display("Print:orderBroken");
    }
}